/**
 *  This file is part of MunchLevel.
 *
 *  MunchLevel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MunchLevel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MunchLevel.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.munchlevel.view;

import java.lang.ref.WeakReference;
import java.security.SecureRandom;
import java.util.Random;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;

import com.munchlevel.R;
import com.munchlevel.util.SoundManager;

public class DiceView extends ImageButton {

	private static final int HANDLE_DICE_6 = 1;

	private Random rnd = new SecureRandom();
	private Animation shakeDice6;
	private DiceHandler mHandler;
	private SoundManager soundManager;

	public DiceView(Context context) {
		this(context, null);
	}

	public DiceView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DiceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		if (!isInEditMode()) {
			shakeDice6 = AnimationUtils.loadAnimation(context, R.anim.shake);
			soundManager = new SoundManager(context);
		}
		setFocusable(true);
		setImageResource(R.drawable.dice_six);

		mHandler = new DiceHandler(this);
	}

	public void rollDice6() {
		soundManager.play(R.raw.shake_dice);
		shakeDice6.reset();
		startAnimation(shakeDice6);

		mHandler.sendMessageDelayed(Message.obtain(mHandler, HANDLE_DICE_6, 7), 200);

	}

	@Override
	protected void onDetachedFromWindow() {
		soundManager.release();
		super.onDetachedFromWindow();
	}

	private void showDice6(int rolls) {

		int value = rnd.nextInt(6);

		getDrawable().setLevel(value);

		if (rolls > 0) {
			mHandler.sendMessageDelayed(Message.obtain(mHandler, HANDLE_DICE_6, rolls - 1), 200);
		}

	}

	static class DiceHandler extends Handler {

		WeakReference<DiceView> diceViewRef;

		/**
		 * 
		 */
		public DiceHandler(DiceView diceView) {
			diceViewRef = new WeakReference<DiceView>(diceView);
		}

		@Override
		public void handleMessage(Message msg) {

			Integer result = (Integer) msg.obj;

			switch (msg.what) {
			case HANDLE_DICE_6:
				DiceView diceView = diceViewRef.get();
				if (diceView != null) {
					diceView.showDice6(result);
				}
				break;
			}
		}
	}

}
